package com.haitaoit.chengquan.module.center.adapter.xuqiu;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.module.center.model.MyXuqiuModel;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/10/12.
 */
public class MyXuqiuAdapter extends BaseAdapter {
    private Context context;
    private BackCall backCall;
    private List<MyXuqiuModel> list;

    public void setBackCall(BackCall backCall) {
        this.backCall = backCall;
    }

    public MyXuqiuAdapter(Context context) {
        this.context = context;
    }

    public List<MyXuqiuModel> getList() {
        return list;
    }

    public void setList(List<MyXuqiuModel> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.my_xuqiu_item, null);
            holder = new ViewHolder();
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.xuqiu_itemll:
                    case R.id.xuqiu_info_ll:
                        MyXuqiuAdapter.this.backCall.deal(R.id.xuqiu_itemll, position);
                        break;
                    case R.id.cancel_xuqiu:
                        MyXuqiuAdapter.this.backCall.deal(R.id.cancel_xuqiu, position);
                        break;
                    case R.id.select_service:
                        MyXuqiuAdapter.this.backCall.deal(R.id.select_service, position);
                        break;
                    case R.id.look_offer:
                        MyXuqiuAdapter.this.backCall.deal(R.id.look_offer, position);
                        break;
                    case R.id.finish:
                        MyXuqiuAdapter.this.backCall.deal(R.id.finish, position);
                        break;
                    case R.id.to_me_eva:
                        MyXuqiuAdapter.this.backCall.deal(R.id.to_me_eva, position);
                        break;
                    case R.id.go_eva:
                        MyXuqiuAdapter.this.backCall.deal(R.id.go_eva, position);
                        break;
                    case R.id.detele_xuqiu:
                        MyXuqiuAdapter.this.backCall.deal(R.id.detele_xuqiu, position);
                        break;
                }
            }
        };

        holder.xuqiu_itemll.setOnClickListener(listener);// 详情
        holder.xuqiu_info_ll.setOnClickListener(listener);// 需求信息
        holder.cancel_xuqiu.setOnClickListener(listener);
        holder.select_service.setOnClickListener(listener);
        holder.look_offer.setOnClickListener(listener);
        holder.finish.setOnClickListener(listener);
        holder.to_me_eva.setOnClickListener(listener);
        holder.go_eva.setOnClickListener(listener);
        holder.detele_xuqiu.setOnClickListener(listener);

        holder.cancel_xuqiu.setVisibility(View.GONE);// 取消需求
        holder.select_service.setVisibility(View.GONE);// 选择服务商
        holder.look_offer.setVisibility(View.GONE);// 查看报价
        holder.finish.setVisibility(View.GONE);// 服务完成
        holder.to_me_eva.setVisibility(View.GONE);// 服务商对我的评价
        holder.go_eva.setVisibility(View.GONE);// 评价服务商
        holder.detele_xuqiu.setVisibility(View.GONE);// 删除订单
        holder.xuqiu_ll.setVisibility(View.VISIBLE);

        /**
         * order_status:订单状态1未报价(显示删除),
         * 2已报价(显示选择服务商和查看报价和取消订单)3已支付(判断是否服务显示服务完成),
         * 4已完成(服务商对我的评价和评价服务商,如果评价完成就显示删除)5已取消(显示删除)
         */
        if (list.get(position).getOrder_status().equals("1")) {
            holder.xuqiu_state.setText("未报价");
            holder.cancel_xuqiu.setVisibility(View.VISIBLE);// 删除订单
        } else if (list.get(position).getOrder_status().equals("2")) {
            holder.xuqiu_state.setText("已报价");
//            holder.cancel_xuqiu.setVisibility(View.VISIBLE);// 取消需求
            holder.select_service.setVisibility(View.VISIBLE);// 选择服务商
            holder.look_offer.setVisibility(View.VISIBLE);// 查看报价
        } else if (list.get(position).getOrder_status().equals("3")) {
            holder.xuqiu_state.setText("已支付");
            // is_service:是否服务1否2是
//            if (list.get(position).getIs_service().equals("1")) {
//                holder.xuqiu_ll.setVisibility(View.GONE);
//            } else {
//                holder.xuqiu_ll.setVisibility(View.VISIBLE);
//                holder.finish.setVisibility(View.VISIBLE);// 服务完成
//            }
            holder.xuqiu_ll.setVisibility(View.VISIBLE);

        } else if (list.get(position).getOrder_status().equals("4")) {
            holder.xuqiu_state.setText("已完成");
            // view_evaluation:1不显示服务商对我的评价2显示服务商对我的评价
            if (list.get(position).getView_evaluation().equals("1")) {
                holder.to_me_eva.setVisibility(View.GONE);
            } else {
                holder.to_me_eva.setVisibility(View.VISIBLE);// 服务商对我的评价
            }
            if (list.get(position).getIs_evaluation().equals("1")) {// is_evaluation:1可以评价2不可以评价
                holder.go_eva.setVisibility(View.VISIBLE);// 评价服务商
            } else {
                holder.detele_xuqiu.setVisibility(View.VISIBLE);// 删除订单
            }
        } else if (list.get(position).getOrder_status().equals("5")) {
            holder.xuqiu_state.setText("已取消");
            holder.detele_xuqiu.setVisibility(View.VISIBLE);// 删除订单
        }

        holder.demand_number.setText("需求编号：" + list.get(position).getDemand_number());
        ImageLoader.getInstance().displayImage(list.get(position).getTitle_img(), holder.title_img);
        holder.demand_title.setText(list.get(position).getTitle());
        holder.type.setText("分类：" + list.get(position).getType());

//        MyXuqiuItemAdapter adapter2 = new MyXuqiuItemAdapter(context);
//        holder.scrollview_listview.setAdapter(adapter2);
//        holder.scrollview_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
//                MyXuqiuAdapter.this.backCall.deal(R.id.scrollview_listview, position);
//            }
//        });

        // 部分字体变大变色
        SpannableString msp = new SpannableString("参考价格：¥ " + list.get(position).getReference_price());
        msp.setSpan(new ForegroundColorSpan(Color.BLACK), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        msp.setSpan(new RelativeSizeSpan(1.5f), 6, ("参考价格：¥ " + list.get(position).getReference_price()).length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        holder.xuqiu_money.setText(msp);
        return convertView;
    }

    class ViewHolder {
        @ViewInject(R.id.xuqiu_itemll)
        LinearLayout xuqiu_itemll;
        @ViewInject(R.id.xuqiu_info_ll)
        LinearLayout xuqiu_info_ll;
        //@ViewInject(R.id.scrollview_listview)
        //MyListView scrollview_listview;
        @ViewInject(R.id.xuqiu_state)
        TextView xuqiu_state;
        @ViewInject(R.id.cancel_xuqiu)
        TextView cancel_xuqiu;
        @ViewInject(R.id.select_service)
        TextView select_service;
        @ViewInject(R.id.look_offer)
        TextView look_offer;
        @ViewInject(R.id.finish)
        TextView finish;
        @ViewInject(R.id.to_me_eva)
        TextView to_me_eva;
        @ViewInject(R.id.go_eva)
        TextView go_eva;
        @ViewInject(R.id.detele_xuqiu)
        TextView detele_xuqiu;
        @ViewInject(R.id.xuqiu_money)
        TextView xuqiu_money;
        @ViewInject(R.id.demand_number)
        TextView demand_number;
        @ViewInject(R.id.title_img)
        ImageView title_img;
        @ViewInject(R.id.demand_title)
        TextView demand_title;
        @ViewInject(R.id.type)
        TextView type;
        @ViewInject(R.id.xuqiu_ll)
        LinearLayout xuqiu_ll;
    }
}
